
// Using the closure to map jQuery to $. 
(function ($) {

// Store our function as a property of Drupal.behaviors.
Drupal.behaviors.deployment = {
	attach: function(context, settings) {
		// We'll use this to cache the progress bar node
        var pbar;
        var started = false;
        // Start progress tracking when the form is submitted
        $('.progressing-upload').submit(function() {

            // Hide the form
            //$('#add-ticket-form').hide();

            // Cache the progress bar
            pbar = $('#progress-bar');

            // Show the progress bar
            // Initialize the jQuery UI plugin
            pbar.progressbar();

			started = true;

            // Start updating progress after a 1 second delay
            setTimeout(function () {

                // We pass the upload identifier to our function
                updateProgress($('#upload-id').val());

            }, 500);

        });


        function updateProgress(id) {

            var time = new Date().getTime();

            // Make a GET request to the server
            // Pass our upload identifier as a parameter
            // Also pass current time to prevent caching
            //$.get('get_package_upload_progress', { upload_id: id, t: time },
            $.ajax({
				url: 'get_package_upload_progress/?upload_id='+id+'&t='+time, 
				success: function(data) {
					// Get the output as an integer
	                var progress = parseInt(data, 10);

	                //if (progress < 100 || !started) {

	                    // Determine if upload has started
	                    //started = progress < 100;

	                    // If we aren't done or started, update again
//	                    updateProgress(id);

	                //}
	                pbar.progressbar('value', progress);
				},
				async: false,
				complete: function() {
					// Schedule the next request when the current one's complete
					setTimeout(function() {updateProgress(id);}, 500);
				},
            });

        }
	}
};
}(jQuery));


